package com.gouwudai.gobbs.service.impl;

import java.util.HashMap;

import com.gouwudai.gobbs.dao.ISysInfoDAO;
import com.gouwudai.gobbs.pojo.TbbsSysInfo;
import com.gouwudai.gobbs.service.IEmailService;
import com.litt.core.common.Utility;
import com.litt.core.mail.SendEmail;
import com.litt.core.util.ParamUtils;

public class EmailServiceImpl implements IEmailService {
	
	ISysInfoDAO sysInfoDAO;
	
	public SendEmail init()
	{
		//获取系统邮件服务器信息		
		TbbsSysInfo tbbsSysInfo = sysInfoDAO.load();
		String smtpId = tbbsSysInfo.getSmtpId();
		String smtpPass =  tbbsSysInfo.getSmtpPass();
		//String sysEmail = tbbsSysInfo.getSysEmail();
		//String sysEmailSign = tbbsSysInfo.getSysEmailSign();
		String smtpServer = tbbsSysInfo.getSmtpServer();
		int smtpPort = Utility.parseInt(tbbsSysInfo.getSmtpPort(),25);
		boolean smtpAuth = tbbsSysInfo.getSmtpAuth().booleanValue();
		boolean smtpSsl = tbbsSysInfo.getSmtpSsl().booleanValue();
		SendEmail sendEmail = new SendEmail();
		sendEmail.setSmtpServer(smtpServer);
		sendEmail.setSmtpPort(smtpPort);
		sendEmail.setSmtpUser(smtpId);
		sendEmail.setSmtpPwd(smtpPass);
		sendEmail.setNeedAuth(smtpAuth);
		sendEmail.setNeedSSL(smtpSsl);	
		return sendEmail;
	}	
	
	/**
	 * 直接发送,还需要设置相关参数
	 * @throws Exception
	 */
	public void sendMail(SendEmail sendEmail) throws Exception
	{
		sendEmail.sendMail();		
	}
	
	/**
	 * 主题浏览中发送邮件给用户
	 * @param paramMap
	 * @return
	 */
	public String topicSend(HashMap paramMap,SendEmail sendEmail)
	{
		String ret = "";
		try
		{
			String mailFrom = ParamUtils.getMapToString(paramMap, "mailFrom");
			String mailSign = ParamUtils.getMapToString(paramMap, "mailSign");			
			String mailTo = ParamUtils.getMapToString(paramMap, "mailTo");
			String ttitle = ParamUtils.getMapToString(paramMap, "ttitle");
			String tcontent = ParamUtils.getMapToString(paramMap, "tcontent");
			
			sendEmail.setMailFrom(mailFrom);
			sendEmail.setMailSign(mailSign);
			sendEmail.setMailTo(mailTo);
			sendEmail.setTtitle(ttitle);
			sendEmail.setTcontent(tcontent);
			sendEmail.sendMail();
		}
		catch(Exception e)
		{
			ret = e.getMessage();
		}
		return ret;
	}

	public void setSysInfoDAO(ISysInfoDAO sysInfoDAO) {
		this.sysInfoDAO = sysInfoDAO;
	}
}
